package com.qumap.item.request;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.IsWidget;
import com.qumap.item.ItemInfo;
import com.qumap.item.ItemOnMap;
import com.qumap.item.content.ObjectOnMapShort;
import com.qumap.item.event.ItemEvent;
import com.qumap.item.event.ItemEventCollection;
import com.qumap.item.request.filter.FilteredObjectsCallback;
import com.qumap.item.request.filter.entry.FilterEntry;
import com.qumap.item.request.load.LoadItemCallback;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Admin
 * Date: 10.06.11
 * Time: 22:15
 * To change this template use File | Settings | File Templates.
 */
public abstract class ItemFilterAndLoadProcessor implements IsWidget {

    protected EventBus global_event_bus;
    private IItemFilterProcessorAsync filter_async_processor;
    protected ItemInfo item_info;

    public abstract IItemFilterProcessorAsync getAsyncProcessor();


    public void setEventBus(EventBus global_event_bus) {
        this.global_event_bus = global_event_bus;
    }

    public void setItemInfo(ItemInfo info) {
        this.item_info = info;
    }


    public void init() {
        filter_async_processor = getAsyncProcessor();
    }

    public void loadItem(ItemOnMap container) {
        LoadItemCallback callback = new LoadItemCallback(this);
        callback.setContainerObject(container);
        filter_async_processor.loadObject(container.getContent().getId(), callback);

    }

    protected void processFilter(List<FilterEntry> filters) {
        FilteredObjectsCallback callback = new FilteredObjectsCallback(this);
        filter_async_processor.getFilteredObjects(filters, callback);
    }

    public void onContentLoad(List<ObjectOnMapShort> map_object) {
        ItemEvent new_item_event = ItemEventCollection.getCollection().getEvent(ItemEventCollection.EventType.Load);
        for(ObjectOnMapShort loaded_content: map_object) {
            ItemOnMap loaded_item = createItemForLoadedObject(loaded_content);
            new_item_event.add(loaded_item);
        }
        global_event_bus.fireEvent(new_item_event);
    }

    private ItemOnMap createItemForLoadedObject(ObjectOnMapShort object) {
        ItemOnMap loaded_item = new ItemOnMap(item_info);
        ObjectOnMapShort item_content = item_info.createContent();
        object.putTo(item_content);
        loaded_item.setContent(item_content);
        return loaded_item;
    }

    public void onFailure(Throwable throwable) {
        Window.alert("Can t filter items, reason " + throwable);
    }

    public void onRequestedObjectLoad(ItemOnMap container, ObjectOnMapShort content) {
        container.setContent(content);
        container.setFullyLoad();

        ItemEvent item_load = ItemEventCollection.getCollection().getEvent(ItemEventCollection.EventType.Load);
        item_load.add(container);
        global_event_bus.fireEvent(item_load);


        ItemEvent select_event = ItemEventCollection.getCollection().getEvent(ItemEventCollection.EventType.Select);
        select_event.add(container);
        global_event_bus.fireEvent(select_event);
    }


}
